import {
    getInitLabels,
    skipInitLabelSelect,
    initLabelSelect
} from "../../requestFunctions/labelRequestFunctions";
const app = getApp();

Component({
    externalClasses: [],
    properties: {},
    data: {
        opacity: 0,
        labels: null,
        labelSelected: null,
        selectedCount: null
    },
    methods: {
        closeButtonTapped: function (e) {
            this._closeModal();
        },
        onLabelTapped: function (e) {
            const labelIndex = e.target.dataset.labelIndex;
            const labelSelected = this.data.labelSelected;
            labelSelected[labelIndex] = !labelSelected[labelIndex];
            this.setData({
                labelSelected: labelSelected,
                selectedCount: labelSelected.filter(a => a).length,
            });
        },
        onRefreshTapped: function () {
            const that = this;
            getInitLabels().then(labels => {
                const labelSelected = labels.map(label => false);
                that.setData({
                    labels: labels,
                    labelSelected: labelSelected,
                    selectedCount: 0,
                });
            });
        },
        onSelectTapped: function () {
            const that = this;
            initLabelSelect(app.globalData.faiUserInfo.faiSessionKey, this.data.labels.filter((label, index) => that.data.labelSelected[index]).map(a=>a.id)).then(()=>{
                that._closeModal();
            });
        },
        onSkipTapped: function () {
            const that = this;
            skipInitLabelSelect(app.globalData.faiUserInfo.faiSessionKey).then(() => {
                that._closeModal();
            });
        },
        _closeModal: function () {
            const that = this;
            this.setData({
                opacity: 0
            }, () => {
                setTimeout(() => {
                    that.triggerEvent('close');
                }, 500);
            });
        }
    },
    created: function (options) {
        // console.log('selectLabelsModal', 'created', this);
        // 组件生命周期函数，在组件实例进入页面节点树时执行，注意此时不能调用 setData
    },
    attached: function () {
        // console.log('selectLabelsModal', 'attached', this);
        // 组件生命周期函数，在组件实例进入页面节点树时执行
    },
    ready: function () {
        this.setData({
            opacity: 1,
        });
        const that = this;
        getInitLabels().then(labels => {
            const labelSelected = labels.map(label => false);
            that.setData({
                labels: labels,
                labelSelected: labelSelected,
                selectedCount: 0,
            });
        });
        // console.log('selectLabelsModal', 'ready', this);
        // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息（使用 SelectorQuery）
    },
    moved: function () {
        // console.log('selectLabelsModal', 'moved', this);
        // 组件生命周期函数，在组件实例被移动到节点树另一个位置时执行
    },
    detached: function () {
        // console.log('selectLabelsModal', 'detached', this);
        // 组件生命周期函数，在组件实例被从页面节点树移除时执行
    },
})